/*
 
 */
package com.ohos.connent.accountcloud.sys.modular.role.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ohos.connent.accountcloud.common.annotation.CommonLog;
import com.ohos.connent.accountcloud.common.pojo.CommonResult;
import com.ohos.connent.accountcloud.common.pojo.CommonValidList;
import com.ohos.connent.accountcloud.sys.modular.role.entity.SysRole;
import com.ohos.connent.accountcloud.sys.modular.role.param.*;
import com.ohos.connent.accountcloud.sys.modular.role.result.*;
import com.ohos.connent.accountcloud.sys.modular.role.service.SysRoleService;
import com.ohos.connent.accountcloud.sys.modular.user.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色控制器
 *
 *
 */
@Api(tags = "角色控制器")
@ApiSupport(author = "OHOSCONNECT_TEAM", order = 8)
@RestController
@Validated
@RequestMapping("/api/v1/account/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 获取角色分页
     *
     *
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取角色分页")
    @PostMapping("/page")
    public CommonResult<Page<SysRoleResult>> page(@RequestBody @Valid SysRolePageParam sysRolePageParam) {
        Page<SysRole>  page = sysRoleService.page(sysRolePageParam);
        Page<SysRoleResult> resultPage = new Page<>();
        resultPage.setCurrent(page.getCurrent());
        resultPage.setSize(page.getSize());
        resultPage.setTotal(page.getTotal());
        resultPage.setPages(page.getPages());
        List<SysRoleResult> results = new ArrayList<>();
        page.getRecords().forEach(sysRole -> {
            SysRoleResult sysUserResult = BeanUtil.toBean(sysRole,SysRoleResult.class);
            if(sysRole.getExtJson() != null){
                String remark = JSONUtil.parseObj(sysRole.getExtJson()).getStr("description");
                sysUserResult.setRemark(remark);
            }
            sysUserResult.setPermissionList(sysRoleService.ownPermissionEx(sysRole.getId()));
            results.add(sysUserResult);
        });

        resultPage.setRecords(results);
        return CommonResult.data(resultPage);
    }

    /**
     * 添加角色
     *
     *
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加角色")
    @CommonLog("添加角色")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid SysRoleAddParam sysRoleAddParam) {
        sysRoleAddParam.setCategory("ORG");
        sysRoleAddParam.setOrgId("1543842934270394370");
        sysRoleAddParam.setExtJson("{}");
        sysRoleAddParam.setSortCode(2);
        if(BeanUtil.isNotEmpty(sysRoleAddParam.getRemark())){
            String extJsonTampl= "{\"description\":\"####\"}";
            sysRoleAddParam.setExtJson(extJsonTampl.replace("####",sysRoleAddParam.getRemark()));
        }
        sysRoleService.add(sysRoleAddParam);
        return CommonResult.ok();
    }

//    /**
//     * 编辑角色
//     *
//     *
//     */
//    @ApiOperationSupport(order = 3)
//    @ApiOperation("编辑角色")
//    @CommonLog("编辑角色")
//    @PostMapping("/edit")
//    public CommonResult<String> edit(@RequestBody @Valid SysRoleEditParam sysRoleEditParam) {
//        sysRoleService.edit(sysRoleEditParam);
//        return CommonResult.ok();
//    }


    /**
     * 编辑角色
     *
     *
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑角色")
    @CommonLog("编辑角色")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid SysRoleEditParam sysRoleEditParam) {
        sysRoleService.edit(sysRoleEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除角色
     *
     *
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除角色")
    @CommonLog("删除角色")
    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<String> sysRoleIdParamList) {
        sysRoleService.delete(sysRoleIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取角色详情
     *
     *
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取角色详情")
    @GetMapping("/detail")
    public CommonResult<SysRole> detail(@Valid SysRoleIdParam sysRoleIdParam) {
        return CommonResult.data(sysRoleService.detail(sysRoleIdParam));
    }

    /**
     * 获取角色拥有资源
     *
     *
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("获取角色拥有资源")
    @GetMapping("/ownResource")
    public CommonResult<SysRoleOwnResourceResult> ownResource(@Valid SysRoleIdParam sysRoleIdParam) {
        return CommonResult.data(sysRoleService.ownResource(sysRoleIdParam));
    }

    /**
     * 给角色授权资源
     *
     *
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("给角色授权资源")
    @CommonLog("给角色授权资源")
    @PostMapping("/grantResource")
    public CommonResult<String> grantResource(@RequestBody @Valid SysRoleGrantResourceParam sysRoleGrantResourceParam) {
        sysRoleService.grantResource(sysRoleGrantResourceParam);
        return CommonResult.ok();
    }

    /**
     * 获取角色拥有移动端菜单
     *
     *
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation("获取角色拥有移动端菜单")
    @GetMapping("/sys/role/ownMobileMenu")
    public CommonResult<SysRoleOwnMobileMenuResult> ownMobileMenu(@Valid SysRoleIdParam sysRoleIdParam) {
        return CommonResult.data(sysRoleService.ownMobileMenu(sysRoleIdParam));
    }

    /**
     * 给角色授权移动端菜单
     *
     *
     */
    @ApiOperationSupport(order = 9)
    @ApiOperation("给角色授权移动端菜单")
    @CommonLog("给角色授权移动端菜单")
    @PostMapping("/sys/role/grantMobileMenu")
    public CommonResult<String> grantMobileMenu(@RequestBody @Valid SysRoleGrantMobileMenuParam sysRoleGrantMobileMenuParam) {
        sysRoleService.grantMobileMenu(sysRoleGrantMobileMenuParam);
        return CommonResult.ok();
    }

    /**
     * 获取角色拥有权限
     *
     *
     */
//    @ApiOperationSupport(order = 10)
//    @ApiOperation("获取角色拥有权限")
//    @GetMapping("/ownPermission")
//    public CommonResult<SysRoleOwnPermissionResult> ownPermission(@Valid SysRoleIdParam sysRoleIdParam) {
//        return CommonResult.data(sysRoleService.ownPermission(sysRoleIdParam));
//    }

    @ApiOperationSupport(order = 10)
    @ApiOperation("获取角色拥有权限")
    @GetMapping("/ownPermission")
    public CommonResult<List<SysRolePermissions>> ownPermission(@Valid SysRoleIdParam sysRoleIdParam) {
        return CommonResult.data(sysRoleService.ownPermissionEx(sysRoleIdParam.getId()));
    }

    /**
     * 给角色授权权限
     *
     *
     */
//    @ApiOperationSupport(order = 11)
//    @ApiOperation("给角色授权权限")
//    @CommonLog("给角色授权权限")
//    @PostMapping("/grantPermission")
//    public CommonResult<String> grantPermission(@RequestBody @Valid SysRoleGrantPermissionParam sysRoleGrantPermissionParam) {
//        sysRoleService.grantPermission(sysRoleGrantPermissionParam);
//        return CommonResult.ok();
//    }

    @ApiOperationSupport(order = 11)
    @ApiOperation("给角色授权权限")
    @CommonLog("给角色授权权限")
    @PostMapping("/grantPermission")
    public CommonResult<String> grantPermission(@RequestBody @Valid SysRoleGrantPermissionsExParam sysRoleGrantPermissionsExParam) {
        sysRoleService.grantPermissionEx(sysRoleGrantPermissionsExParam);
        return CommonResult.ok();
    }

    /**
     * 获取角色下的用户
     *
     *
     */
    @ApiOperationSupport(order = 12)
    @ApiOperation("获取角色下的用户")
    @GetMapping("/ownUser")
    public CommonResult<List<String>> ownUser(@Valid SysRoleIdParam sysRoleIdParam) {
        return CommonResult.data(sysRoleService.ownUser(sysRoleIdParam));
    }

    /**
     * 给角色授权用户
     *
     *
     */
    @ApiOperationSupport(order = 13)
    @ApiOperation("给角色授权用户")
    @CommonLog("给角色授权用户")
    @PostMapping("/grantUser")
    public CommonResult<String> grantUser(@RequestBody @Valid SysRoleGrantUserParam sysRoleGrantUserParam) {
        sysRoleService.grantUser(sysRoleGrantUserParam);
        return CommonResult.ok();
    }

    /* ====角色部分所需要用到的选择器==== */

    /**
     * 获取组织树选择器
     *
     *
     */
    @ApiOperationSupport(order = 14)
    @ApiOperation("获取组织树选择器")
    @GetMapping("/sys/role/orgTreeSelector")
    public CommonResult<List<Tree<String>>> orgTreeSelector() {
        return CommonResult.data(sysRoleService.orgTreeSelector());
    }

    /**
     * 获取资源授权树
     *
     *
     */
    @ApiOperationSupport(order = 15)
    @ApiOperation("获取资源授权树")
    @GetMapping("/sys/role/resourceTreeSelector")
    public CommonResult<List<SysRoleGrantResourceTreeResult>> resourceTreeSelector() {
        return CommonResult.data(sysRoleService.resourceTreeSelector());
    }

    /**
     * 获取移动端菜单授权树
     *
     *
     */
    @ApiOperationSupport(order = 16)
    @ApiOperation("获取移动端菜单授权树")
    @GetMapping("/sys/role/mobileMenuTreeSelector")
    public CommonResult<List<SysRoleGrantMobileMenuTreeResult>> mobileMenuTreeSelector() {
        return CommonResult.data(sysRoleService.mobileMenuTreeSelector());
    }

    /**
     * 获取权限授权树
     *
     *
     */
    @ApiOperationSupport(order = 17)
    @ApiOperation("获取权限授权树")
    @GetMapping("/sys/role/permissionTreeSelector")
    public CommonResult<List<String>> permissionTreeSelector() {
        return CommonResult.data(sysRoleService.permissionTreeSelector());
    }

    /**
     * 获取角色选择器
     *
     *
     */
    @ApiOperationSupport(order = 18)
    @ApiOperation("获取角色选择器")
    @GetMapping("/sys/role/roleSelector")
    public CommonResult<Page<SysRole>> roleSelector(SysRoleSelectorRoleParam sysRoleSelectorRoleParam) {
        return CommonResult.data(sysRoleService.roleSelector(sysRoleSelectorRoleParam));
    }

    /**
     * 获取用户选择器
     *
     *
     */
    @ApiOperationSupport(order = 19)
    @ApiOperation("获取用户选择器")
    @GetMapping("/sys/role/userSelector")
    public CommonResult<Page<SysUser>> userSelector(SysRoleSelectorUserParam sysRoleSelectorUserParam) {
        return CommonResult.data(sysRoleService.userSelector(sysRoleSelectorUserParam));
    }
}
